package 设计模式.proxy.dynamicproxy;

import org.springframework.cglib.core.DebuggingClassWriter;
import sun.misc.ProxyGenerator;


import java.io.*;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * @功能
 * @创建时间 2020/5/3
 */
public class Client {

    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            if (map.get(target - nums[i]) != null){
                return new int[]{map.get(target - nums[i]),i};
            }
            map.put(nums[i],i);
        }
        return new int[]{};
    }
    public static void main(String[] args) throws IOException {
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "/Users/wuxiangqi/Desktop/pratice");
        Star realstar= new RealStar();
        StarHandler handler = new StarHandler(realstar);
        Star proxy = (Star) Proxy.newProxyInstance(//ClassLoader.getSystemClassLoader()
                Star.class.getClassLoader(),new Class[]{Star.class},handler);
        // java9后不在支持
        byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
                "proxyName.class", new Class[]{Star.class});

        OutputStream outputStream = new FileOutputStream("/Users/wuxiangqi/Desktop/pratice/proxyName1.class");
        outputStream.write(proxyClassFile);
        outputStream.close();
        proxy.bookTicket();
        proxy.sing();
    }

    public static byte[] ObjectToByte(Object obj) {
        byte[] bytes = null;
        try {
            // object to bytearray
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(obj);

            bytes = bo.toByteArray();

            bo.close();
            oo.close();
        } catch (Exception e) {
     //       log.info("【BeanToByteUtil】translation->{}:e->{}" , e.getMessage(),e);
            e.printStackTrace();
        }
        return bytes;
    }

}
